package com.css.fxfzxczh.modules.boundary.repository;

import com.css.fxfzxczh.modules.boundary.entity.BoundaryEntity;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2023年06月01日  15:12
 */
@Repository
public class BoundaryRepository {

    @Resource
    JdbcTemplate jdbcTemplate;
    public List<String> getCountyBoundaryList(String geom){
         String sql = "select ST_Intersection(geom,st_geometryfromtext(?,4490)) from district_boundary_county t  where ST_Intersects(geom,st_geometryfromtext(?,4490))";
         List<String> list = jdbcTemplate.queryForList(sql, String.class, geom,geom);
         return  list;
    }

    public List<BoundaryEntity> getProvinceBoundaryList(String geom) {
        String sql = "select t.code,t.name from district_boundary_province T  where ST_Intersects(geom,st_geometryfromtext(?,4490))";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(BoundaryEntity.class), geom);
    }

    public List<BoundaryEntity> getByCode(String code, String level) {
        StringBuilder builder = new StringBuilder("select * from ");
        if("2".equals(level)){
            //查询地市表
            String prefixCode = code.substring(0, 2);
            builder.append(" district_boundary_city where code like '").append(prefixCode).append("%'");
        }
        if("3".equals(level)){
            //查询区县
            String prefixCode = code.substring(0, 4);
            builder.append(" district_boundary_county where code like '").append(prefixCode).append("%'");
        }
        return jdbcTemplate.query(builder.toString(),new BeanPropertyRowMapper<>(BoundaryEntity.class));
    }
    public BoundaryEntity getOneByCode(String code, String level) {
        StringBuilder builder = new StringBuilder("select * from ");
        if("2".equals(level)){
            //查询地市表
            builder.append(" district_boundary_city where code = '").append(code).append("'");
        }
        if("3".equals(level)){
            //查询区县
            builder.append(" district_boundary_county where code = '").append(code).append("'");
        }
        List<BoundaryEntity> query = jdbcTemplate.query(builder.toString(), new BeanPropertyRowMapper<>(BoundaryEntity.class));
        return PlatformObjectUtils.isEmpty(query)?null:query.get(0);
    }
    public BoundaryEntity getBundaryEntityByCode(String code, String level) {
        StringBuilder builder = new StringBuilder("select *,st_astext(geom) geom from ");
        if("2".equals(level)){
            //查询地市表
            builder.append(" district_boundary_city where code = '").append(code).append("'");
        }
        if("3".equals(level)){
            //查询区县
            builder.append(" district_boundary_county where code = '").append(code).append("'");
        }
        List<BoundaryEntity> query = jdbcTemplate.query(builder.toString(), new BeanPropertyRowMapper<>(BoundaryEntity.class));
        return PlatformObjectUtils.isEmpty(query)?null:query.get(0);
    }
}
